package outros;

import java.awt.event.MouseEvent;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;

public class CampoMask extends JFormattedTextField {
    private MaskFormatter mascara;

    public CampoMask(String mask) {
        try {
            mascara = new MaskFormatter(mask);
            mascara.setPlaceholderCharacter('_');
        } catch (Exception e) {}
        mascara.install(this);
        if (mask.substring(0, 1).equals("(")) {
            addMouseListener(new java.awt.event.MouseAdapter() {
                @Override
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    eventoRepositor(evt);
                }
            });
        }
    }

    private void eventoRepositor(MouseEvent evt) {
        if (getCaretPosition()==0) {
            setCaretPosition(1);
        }
    }
}